# Makefile for RISC-V Compiler

CXX = g++
CXXFLAGS = -std=c++11 -Wall -O2
BISON = bison
FLEX = flex

# 目标文件
TARGET = compiler
SOURCES = compiler.cpp parser.tab.cpp lexer.cpp SymbolTable.cpp CodeGenerator.cpp Optimizer.cpp typecheck.cpp
OBJECTS = $(SOURCES:.cpp=.o)

# 默认目标
all: $(TARGET)

# 生成可执行文件
$(TARGET): $(OBJECTS)
	$(CXX) $(CXXFLAGS) -o $@ $^

# 生成parser.tab.cpp和parser.tab.h
parser.tab.cpp parser.tab.h: parser.y
	$(BISON) -d parser.y
	mv parser.tab.c parser.tab.cpp
	mv parser.tab.h parser.tab.h
	sed -i '' 's/parser\.tab\.h/parser.tab.h/g' parser.tab.cpp

# 生成lexer.cpp
lexer.cpp: lexer.l parser.tab.h
	$(FLEX) -o lexer.cpp lexer.l

# 编译目标文件
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# 清理
clean:
	rm -f $(TARGET) $(OBJECTS) parser.tab.cpp parser.tab.h lexer.cpp *.s

# 测试
test: $(TARGET)
	./$(TARGET) test.c

# 复杂测试
test-complex: $(TARGET)
	./$(TARGET) test_complex.c

# 安装依赖（macOS）
install-deps:
	brew install bison flex gcc

# 显示帮助
help:
	@echo "Available targets:"
	@echo "  all          - Build the compiler"
	@echo "  clean        - Remove build files"
	@echo "  test         - Run test with test.c"
	@echo "  test-complex - Run test with test_complex.c"
	@echo "  install-deps - Install dependencies (macOS)"
	@echo "  help         - Show this help"

.PHONY: all clean test test-complex install-deps help 